﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using imeem.library;
using System.IO;
using System.Xml;

namespace imeem.api
{
    /// <summary>
    /// ItemsSerializer is a helper class used to deserialize items (music, video, photo, playlists, user).  The standard .NET XmlSerializer
    /// cannot handle the xml schema used by imeem for items.  This is do to the fact that the type of item is defined by an element within the
    /// item schema i.e.  <item><type>music</type></item> 
    /// </summary>
    public class ItemsSerializer : IXmlSerializable
    {
        private List<Item> _items;

        public List<Item> Items
        {
            get
            {
                if (_items == null)
                    _items = new List<Item>();
                return _items;
            }
        }

        public ItemsSerializer(List<Item> items)
        {
            _items = items;
        }

        public ItemsSerializer()
        {

        }

        private void deserializeItem(string type, string itemXml)
        {
            XmlSerializer serializer = ItemSerializerFactory.GetSerializer(type);
            if (serializer != null)
            {  // if itemtype is not supported by this system, serializer will be null
                Item item = serializer.Deserialize(new StringReader(itemXml)) as Item;
                Items.Add(item);
            }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            Stack<string> elemStack = new Stack<string>();

            string xml = reader.ReadOuterXml();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNodeList items = doc.SelectNodes("/items/item");
            XmlNode typeNode;
            string type;

            foreach (XmlNode item in items)
            {
                typeNode = item.SelectSingleNode("./type");
                if (typeNode != null)
                {
                    type = typeNode.InnerText;
                    deserializeItem(type, item.OuterXml);
                }
            }

            #region old shit

            //string itemXml = "";
            //string currentType = "";
            //do
            //{
            //    if (reader.IsStartElement())
            //    {
            //        if (reader.LocalName != "items" || elemStack.Count > 0)
            //        {
            //            if (elemStack.Count > 0 && reader.LocalName == elemStack.Peek())
            //            {
            //                itemXml += "</" + elemStack.Pop() + ">";
            //            }
            //            elemStack.Push(reader.LocalName);
            //            itemXml += "<" + reader.LocalName + ">";
            //        }
            //    }
            //    else
            //    {
            //        string content = reader.ReadString();
            //        content = System.Web.HttpUtility.HtmlEncode(content);
            //        itemXml += content;
            //        itemXml += "</" + reader.LocalName + ">";

            //        if (reader.LocalName == "items" && elemStack.Count == 0)
            //        {
            //            reader.ReadEndElement();
            //            break;
            //        }

            //        elemStack.Pop();

            //        if (reader.LocalName == "type")
            //        {
            //            currentType = content;
            //        }
            //        else if (reader.LocalName == "item" && elemStack.Count == 0)
            //        {
            //            deserializeItem(currentType, itemXml);
            //            itemXml = "";
            //        }
            //        else if (reader.LocalName == "items" && elemStack.Count == 0)
            //        {
            //            reader.ReadEndElement();
            //            break;
            //        }
            //       // elemStack.Pop();
            //    }
            //    if (elemStack.Count == 0) 
            //    {
            //        int i = 0;
            //    }
            //} while (reader.Read());

            #endregion
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
